package cn.thread;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程的第4种实现方式 线程池
 */
public class MyThreadPool implements Runnable{

    @Override
    public void run() {
        for (int i=0;i<100;i++){
            System.out.println(i);
        }
    }
}
//测试
class ThreadPool{
    public static void main(String[] args) {
        //线程池的使用
        ExecutorService service= Executors.newFixedThreadPool(10);
        //设置线程池属性
        ThreadPoolExecutor threadPoolExecutor= (ThreadPoolExecutor) service;
        threadPoolExecutor.setCorePoolSize(10);
        //注册使用线程
        service.execute(new MyThreadPool()); //execute 适合 runnable
        //service.submit(new MyThreadPool()); submit 适合 callable
        service.shutdown();//关闭线程
    }
}